package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.L1DropEnchant;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.Random;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class DropItemEnchantTable {
  private static final Log _log = LogFactory.getLog(DropItemEnchantTable.class);
  
  private static DropItemEnchantTable _instance;
  
  private static final HashMap<Integer, ArrayList<L1DropEnchant>> _dropItemEnchant = new HashMap<>();
  
  public static DropItemEnchantTable get() {
    if (_instance == null)
      _instance = new DropItemEnchantTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `w_怪物掉落随机强化`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int npcid = rs.getInt("npc_id");
        int itemid = rs.getInt("item_id");
        if (ItemTable.get().getTemplate(itemid) == null) {
          _log.error("掉落物品机率资料错误: 没有这个编号的道具:" + itemid);
          errorItem(itemid);
          continue;
        } 
        String dropenchants = rs.getString("drop_enchants").replaceAll(" ", "");
        String[] enchants_tmp = dropenchants.split(",");
        int[] enchants = new int[enchants_tmp.length];
        int i = 0;
        while (i < enchants_tmp.length) {
          enchants[i] = Integer.parseInt(enchants_tmp[i]);
          i++;
        } 
        L1DropEnchant data = new L1DropEnchant(itemid, enchants);
        ArrayList<L1DropEnchant> datalist = _dropItemEnchant
          .get(Integer.valueOf(npcid));
        if (datalist == null)
          datalist = new ArrayList<>(); 
        datalist.add(data);
        _dropItemEnchant.put(Integer.valueOf(npcid), datalist);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log
      .info("载入掉落物品强化值资料数量: " + _dropItemEnchant.size() + "(" + timer.get() + "ms)");
  }
  
  public ArrayList<L1DropEnchant> getDatalist(int npcid) {
    ArrayList<L1DropEnchant> datalist = _dropItemEnchant.get(Integer.valueOf(npcid));
    if (datalist == null)
      return null; 
    return datalist;
  }
  
  public int getEnchant(L1DropEnchant data) {
    int[] enchants = data.getEnchants();
    int level = enchants[Random.nextInt(enchants.length)];
    return level;
  }
  
  private static void errorItem(int itemid) {
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("DELETE FROM `w_怪物掉落随机强化` WHERE `item_id`=?");
      pstm.setInt(1, itemid);
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
}
